<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;

class ClearRedisData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'redis:clear {date?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '清除redis垃圾数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $redis = Redis::connection();
        //清除访问IP数据
        $date = $this->argument('date') ?: Carbon::now()->toDateString();
        $keys = $redis->keys('visit_record:' . $date . '*');
        foreach ($keys as $key) {
            $key = str_replace('{laravel_database}_','',$key);
            $redis->del($key);
        }
    }
}
